package security.access

object PermissionAccess {
  def check(access: Int, permission: String): Boolean = {
    if (access_permissions.contains(access)) {
      access_permissions(access).filter(_.equals(permission)).nonEmpty
    }
    else false
  }

  def check(access: String, permission: String): Boolean = {
    try {
      val accessid = access.toInt
      if (access_permissions.contains(accessid)) {
        access_permissions(accessid).filter(_.equals(permission)).nonEmpty
      }
      else false
    } catch {
      case e: Exception => false
    }
  }

  def getPermission(code:Int)= {
    access_permissions.get(code)
  }

  def getPermission(code: String) = {
    var k = 0
    try {
       k = code.toInt
    }catch {
      case e:Exception =>
    }
    access_permissions.get(k).getOrElse(Seq()).mkString(",")
  }
  protected val access_permissions = Map(
    0 -> Seq("nologin"), // 固定序号不变，无登陆权限
    -1 -> Seq("login", "admin", "wuyang", "lidun","rihua"), // 固定序号不变，管理员，添加全权限
    1 -> Seq("login"), // 固定序号不变，仅登陆权限
    2 -> Seq("login", "wuyang", "lidun","rihua"), // 固定序号不变，全组织
    3 -> Seq("login", "wuyang"), // 单一组织
    4 -> Seq("login", "lidun"),  // 单一组织
    5 -> Seq("login", "rihua"),  // 单一组织
  )

}
